program principal

interface

	function funcion(x)
		real(Kind=8),intent(in)::x
	end function

	function derivada(x)
		real(Kind=8),intent(in)::x
	end function

	subroutine calcular(x,it,tol)
		real(kind=8)::x,ant,tol,error
		integer::it,i
	end subroutine

	subroutine datos(x,maxit,tol)
		real(kind=8)::x,tol
		integer::maxit
	end subroutine

end interface



	real(kind=8)::x,tol
	integer::maxit
	call datos(x,maxit,tol)
	call calcular(x,maxit,tol)


end program


function funcion(x)
	real(Kind=8), intent(in)::x
	!funcion=(-x)**3 + 12*(x**2) - 46*x+56
		funcion= x-1
end function funcion

function derivada(x)
	real(Kind=8), intent(in)::x
	!derivada=(-3)*(x**2) + 24*x-46
derivada=1
end function derivada

	subroutine calcular(x,it,tol)
		real(kind=8)::x,ant,tol,error
		integer::it,i
	
		open(unit=1, file='newton.dat',status='unknown')
		write(1,*)'Aproximacion inicial: ',x
		write(1,*)'Nº max. de iteraciones: ',it
		write(1,*)'Tolerancia: ',tol
		write(1,*)'-------------------------'

		do i=1,it,1
			ant=x
			x=x-funcion(x)/ derivada(x)
			error=abs(x-ant)/abs(x)
			if( error<tolerancia) then
				exit
			end if
			write(1,*)'iteracion: ',i
			write(1,*)'aproximacion: ',x
			write(1,*)'error relativo: ',error
			write(1,*)
		end do	
		close(1)
			
	end subroutine



subroutine datos(x,maxit,tol)
	real(kind=8)::x,tol
	integer::maxit
	print*,'¿aproximación inicial?'
	read(5,*)x
	print*,'¿numero maximo de iteraciones?'
	read(5,*),maxit
	print*,'¿tolerancia de error?'
	read(5,*)tol
end subroutine
